"""
人脸识别服务
"""

import cv2
import numpy as np
import os

RECOGNIZER = cv2.face.LBPHFaceRecognizer_create()  # LBPH识别器
# RECOGNIZER = cv2.faces.EigenFaceRecognizer_create()#特征脸识别器
# RECOGNIZER = cv2.faces.FisherFaceRecognizer_create()  # LBPH识别器
PASS_CONF = 45  # 最高评分，LBPH最高建议用45，特征脸最高建议用22000
FACE_CASCADE = cv2.CascadeClassifier(os.getcwd() + "\\cascades\\haarcascade_frontalface_default.xml")  # 加载人脸识别级联分类器



def train(photos, lables):
    RECOGNIZER.train(photos, np.array(lables))  #



def recognise_face(photo):
    """
    识别器识别图像中的人脸
    :param photo:
    :return:
    """
    label, confidence = RECOGNIZER.predict(photo)
    if confidence > PASS_CONF:
        return -1
    return label



def found_face(gary_img):
    """
    判断图像中是否有正面人脸
    :param gary_img:
    :return:
    """
    faces = FACE_CASCADE.detectMultiScale(gary_img, 1.15, 4)
    return len(faces) > 0
